# 相机百科全书项目架构设计Word文档生成脚本
Write-Host "开始生成Word文档..."

try {
    # 创建Word应用程序对象
    $word = New-Object -ComObject Word.Application
    $word.Visible = $false
    
    # 创建新文档
    $doc = $word.Documents.Add()
    
    # 设置输出文件路径
    $outputFile = "$PSScriptRoot\相机百科全书项目架构设计.docx"
    
    # 添加标题
    $titleRange = $doc.Content
    $titleRange.InsertAfter("相机百科全书项目架构设计")
    $titleRange.Font.Size = 28
    $titleRange.Font.Bold = $true
    $titleRange.ParagraphFormat.Alignment = 1 # 居中对齐
    $doc.Content.InsertParagraphAfter()
    
    # 添加副标题
    $subtitleRange = $doc.Content
    $subtitleRange.Collapse(0) # 移到末尾
    $subtitleRange.InsertAfter("Django 5.2.5 项目")
    $subtitleRange.Font.Size = 18
    $subtitleRange.Font.Bold = $true
    $subtitleRange.ParagraphFormat.Alignment = 1 # 居中对齐
    $doc.Content.InsertParagraphAfter()
    
    # 添加技术栈信息
    $techRange = $doc.Content
    $techRange.Collapse(0)
    $techRange.InsertAfter("技术栈: Python + Django + SQLite + HTML5 + CSS3")
    $techRange.Font.Size = 14
    $techRange.Font.Bold = $true
    $techRange.ParagraphFormat.Alignment = 1 # 居中对齐
    $doc.Content.InsertParagraphAfter()
    
    # 插入分页符
    $doc.Content.Collapse(0)
    $doc.Content.InsertBreak(7) # wdPageBreak
    
    # 1. 数据模型部分
    $modelTitleRange = $doc.Content
    $modelTitleRange.Collapse(0)
    $modelTitleRange.InsertAfter("1. 数据模型层")
    $modelTitleRange.Font.Size = 20
    $modelTitleRange.Font.Bold = $true
    $modelTitleRange.ParagraphFormat.Alignment = 0 # 左对齐
    $doc.Content.InsertParagraphAfter()
    
    # 相机分类
    $cameraCategoryRange = $doc.Content
    $cameraCategoryRange.Collapse(0)
    $cameraCategoryRange.InsertAfter("1.1 相机分类")
    $cameraCategoryRange.Font.Size = 16
    $cameraCategoryRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraCategoryTextRange = $doc.Content
    $cameraCategoryTextRange.Collapse(0)
    $cameraCategoryTextRange.InsertAfter("相机分类是对不同类型相机的归类管理。")
    $doc.Content.InsertParagraphAfter()
    
    # 相机词条
    $cameraItemRange = $doc.Content
    $cameraItemRange.Collapse(0)
    $cameraItemRange.InsertAfter("1.2 相机词条")
    $cameraItemRange.Font.Size = 16
    $cameraItemRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraItemTextRange = $doc.Content
    $cameraItemTextRange.Collapse(0)
    $cameraItemTextRange.InsertAfter("相机词条包含相机的详细信息，如型号、参数、特性等。")
    $doc.Content.InsertParagraphAfter()
    
    # 数据关系表格
    $tableTitleRange = $doc.Content
    $tableTitleRange.Collapse(0)
    $tableTitleRange.InsertAfter("1.5 数据关系")
    $tableTitleRange.Font.Size = 16
    $tableTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    # 创建表格
    $table = $doc.Tables.Add($doc.Content, 4, 2)
    $table.Borders.Enable = $true
    
    # 表头
    $table.Cell(1, 1).Range.Text = "实体关系"
    $table.Cell(1, 2).Range.Text = "关系类型"
    $table.Cell(1, 1).Range.Font.Bold = $true
    $table.Cell(1, 2).Range.Font.Bold = $true
    
    # 表格内容
    $table.Cell(2, 1).Range.Text = "相机分类 - 相机词条"
    $table.Cell(2, 2).Range.Text = "一对多"
    $table.Cell(3, 1).Range.Text = "配件分类 - 相机配件"
    $table.Cell(3, 2).Range.Text = "一对多"
    $table.Cell(4, 1).Range.Text = "相机词条 - 相机配件"
    $table.Cell(4, 2).Range.Text = "多对多 (兼容性)"
    
    $doc.Content.InsertParagraphAfter()
    
    # 2. 视图功能部分
    $viewTitleRange = $doc.Content
    $viewTitleRange.Collapse(0)
    $viewTitleRange.InsertAfter("2. 视图功能层")
    $viewTitleRange.Font.Size = 20
    $viewTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    # 相机视图
    $cameraViewTitleRange = $doc.Content
    $cameraViewTitleRange.Collapse(0)
    $cameraViewTitleRange.InsertAfter("2.1 相机视图")
    $cameraViewTitleRange.Font.Size = 16
    $cameraViewTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraViews = @(
        "相机列表 - 展示所有相机条目",
        "相机详情 - 展示单个相机的详细信息",
        "创建相机 - 添加新的相机条目",
        "编辑相机 - 修改已有相机的信息",
        "删除相机 - 移除相机条目"
    )
    
    foreach ($view in $cameraViews) {
        $viewRange = $doc.Content
        $viewRange.Collapse(0)
        $viewRange.InsertAfter($view)
        $viewRange.Font.Bold = $true
        $doc.Content.InsertParagraphAfter()
    }
    
    # 3. 项目功能概览
    $featureTitleRange = $doc.Content
    $featureTitleRange.Collapse(0)
    $featureTitleRange.InsertAfter("3. 项目功能概览")
    $featureTitleRange.Font.Size = 20
    $featureTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $projectFeatures = @(
        "1. 相机信息展示与管理",
        "2. 相机配件展示与管理",
        "3. 分类浏览与过滤功能",
        "4. 搜索与数据统计",
        "5. 配件与相机兼容性管理"
    )
    
    foreach ($feature in $projectFeatures) {
        $featureRange = $doc.Content
        $featureRange.Collapse(0)
        $featureRange.InsertAfter($feature)
        $featureRange.Font.Bold = $true
        $doc.Content.InsertParagraphAfter()
    }
    
    # 4. 数据统计
    $statsTitleRange = $doc.Content
    $statsTitleRange.Collapse(0)
    $statsTitleRange.InsertAfter("4. 数据统计")
    $statsTitleRange.Font.Size = 20
    $statsTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $statsRange = $doc.Content
    $statsRange.Collapse(0)
    $statsRange.InsertAfter("增强版数据统计")
    $statsRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $doc.Content.Collapse(0)
    $doc.Content.InsertAfter("相机分类: 5个 | 相机条目: 13台")
    $doc.Content.InsertParagraphAfter()
    
    $doc.Content.Collapse(0)
    $doc.Content.InsertAfter("配件分类: 8个 | 配件条目: 16个")
    $doc.Content.InsertParagraphAfter()
    
    # 保存文档
    $doc.SaveAs([ref]$outputFile)
    $doc.Close()
    $word.Quit()
    
    # 释放COM对象
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($table) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
    
    Write-Host "Word文档已成功生成: $outputFile"
} catch {
    Write-Host "生成Word文档时出错: $_"
    # 确保释放Word进程
    if ($word) {
        $word.Quit()
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
    }
}